package org.moyakarta.rest;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PlaceStorageInMemory {

   public synchronized String generateId() {
      String id = PlaceGenerateUtils.generateEncodedHash();
      while (getPlace(id) != null) {
         id = PlaceGenerateUtils.generateEncodedHash();
      }
      return id;
   }

   private Map<String, Place> places = new ConcurrentHashMap<String, Place>();

   public PlaceStorageInMemory() {
//      init();
   }

   private void init() {
      Place place = new Place();
      place.setLat(49.44402965123416);
      place.setLng(32.058283332735314);
      place.setMlat(49.44402965123416);
      place.setMlng(32.058283332735314);
      place.setZoom(13);
      putPlace(place);
   }

   public Place getPlace(String id) {
      return places.get(id);
   }

   public String putPlace(Place place) {
      String hash = place.getHash();
      if (hash == null || hash.trim().length() == 0) {
         hash = generateId();
         place.setHash(hash);
      }
      places.put(hash, place);
      return hash;
   }

   public Collection<Place> getAll() {
      return places.values();
   }

   public int numberOfPlaces() {
      return places.size();
   }
   
}
